## Authors: Alexander Herzog and Kenneth Benoit
## Date: May 30, 2015
## Replication file for JOP article "The Most Unkindest Cuts: Speaker Selection and Expressed Government Dissent During Economic Crisis"

rm(list = ls(all = TRUE))

library(xtable)
library(ggplot2)
library(reshape2)

### PATHS ########################################
dataFile <- "./generated_data/master_data.RData"
##################################################

load(dataFile)

# ==================
# = Case selection =
# ==================
# NOTE: Includes FM and opposition spokesperson

# remove pre-1987 years
data <- data[data$budget_year>=1987,]

# remove Cheann Comhairle and Leas Cheann Comhairle (for safety)
data <- data[!(data$position %in% c("Cheann Comhairle","Leas Cheann Comhairle")),]
data$position <- factor(data$position)

# remove those who were not elected yet (by-elections)
data <- data[data$notElectedYet==0,]

# remove those who resigned or died in office
data <- data[data$removed==0,]

data$opposition <- 0
data$opposition[data$position=="Opposition"] <- 1



# =============
# = Variables =
# =============

# who spoke?
data$spoke <- 0
data$spoke[!is.na(data$textscore)] <- 1

# govt backbenchers
data$backbench <- 0
data$backbench[data$position=="Govt backbencher"] <- 1



# ===================================
# = Table for full Dail composition =
# ===================================
# table frame with budget years
n <- 166

t <- data.frame(budget.year=names(table(data$budget_year)), obs=as.numeric(table(data$budget_year)))

# length of debate in days
t <- data.frame(t, debate.days=with(data, aggregate(debate.days, list(budget_year), mean))[,2])

# number of speakers
t <- data.frame(t, N.speakers=with(data, aggregate(spoke, list(budget_year), sum))[,2])


# cabinet members
t <- data.frame(t, C=with(data, aggregate(cabMember, list(budget_year), sum))[,2])

# cabinet members who spoke
t <- data.frame(t, CS=with(data[data$spoke==1, ], aggregate(cabMember, list(budget_year), sum))[,2])

# cabinet members who spoke, percentage on all speakers 
t$CSP <- as.character(paste0("(",round(t$CS/t$N.speakers*100, digits=0),")"))

t$space1 <- NA


# backbenchers
t <- data.frame(t, B=with(data, aggregate(backbench, list(budget_year), sum))[,2])

# backbenchers who spoke
t <- data.frame(t, BS=with(data[data$spoke==1, ], aggregate(backbench, list(budget_year), sum))[,2])

# backbenchers who spoke, percentage on all speakers
t$BSP <- as.character(paste0("(",round(t$BS/t$N.speakers*100, digits=0),")"))

t$space2 <- NA


# opposition members
t <- data.frame(t, O=with(data, aggregate(opposition, list(budget_year), sum))[,2])

# opposition members who spoke
t <- data.frame(t, OS=with(data[data$spoke==1, ], aggregate(opposition, list(budget_year), sum))[,2])

# opposition members who spoke, percentage on all speakers
t$OSP <- as.character(paste0("(",round(t$OS/t$N.speakers*100, digits=0),")"))

t$space1 <- NA


# make latex table (content only)
x <- xtable(t, digits=0)

print(x,
      only.contents=TRUE,
      hline.after=c(-1),
      include.rownames=FALSE,
      include.colnames=FALSE,
      file="./tables/dail_composition.tex")



# =================================
# = Table for speaker composition =
# =================================
# table frame with budget years
t <- data.frame(budget.year=names(table(data$budget_year)), obs=as.numeric(table(data$budget_year)))

# length of debate in days
t <- data.frame(t, debate.days=with(data, aggregate(debate.days, list(budget_year), mean))[,2])

# number of speakers
t <- data.frame(t, N.speakers=with(data, aggregate(spoke, list(budget_year), sum))[,2])


# number of cabinet members who spoke
t <- data.frame(t, CS=with(data[data$spoke==1, ], aggregate(cabMember, list(budget_year), sum))[,2])

# percentage of cabinet members who spoke
t$CSP <- as.character(paste0("(",round(t$CS/t$N.speakers*100, digits=0),")"))

t$space1 <- NA


# number of backbenchers who spoke
t <- data.frame(t, BS=with(data[data$spoke==1, ], aggregate(backbench, list(budget_year), sum))[,2])

# percentage of backbenchers who spoke
t$BSP <- as.character(paste0("(",round(t$BS/t$N.speakers*100, digits=0),")"))

t$space2 <- NA


# number of opposition members who spoke
t <- data.frame(t, OS=with(data[data$spoke==1, ], aggregate(opposition, list(budget_year), sum))[,2])

# percentage of opposition members who spoke
t$OSP <- as.character(paste0("(",round(t$OS/t$N.speakers*100, digits=0),")"))

t$space1 <- NA


# make latex table (content only)
x <- xtable(t, digits=0)

print(x,
      only.contents=TRUE,
      hline.after=c(-1),
      include.rownames=FALSE,
      include.colnames=FALSE,
      file="./tables/speaker_composition.tex")
